package com.gitee.wsl.doc.prosemirror.history

import com.gitee.wsl.doc.prosemirror.history.bean.Branch
import com.gitee.wsl.doc.prosemirror.history.bean.HistoryState
import com.gitee.wsl.doc.prosemirror.history.ext.applyTransaction
import com.gitee.wsl.doc.prosemirror.state.EditorStateConfig
import com.gitee.wsl.doc.prosemirror.state.EditorState
import com.gitee.wsl.doc.prosemirror.plugin.PluginKey
import com.gitee.wsl.doc.prosemirror.plugin.PluginSpec
import com.gitee.wsl.doc.prosemirror.plugin.StateField
import com.gitee.wsl.doc.prosemirror.state.Transaction

class HistoryPluginSpec(conf: HistoryOptions) : PluginSpec<HistoryState>() {
    override val state: StateField<HistoryState> = object : StateField<HistoryState> {
        override fun init(config: EditorStateConfig, instance: EditorState): HistoryState {
            return HistoryState(Branch.empty, Branch.empty, null, 0, -1)
        }

        override fun apply(
            tr: Transaction,
            value: HistoryState,
            oldState: EditorState,
            newState: EditorState
        ): HistoryState {
            return value.applyTransaction( oldState, tr, conf)
        }
    }

    override val key: PluginKey<HistoryState> = historyKey

    override val additionalProps: Map<String, Any> = mapOf(
        "config" to conf
    )

    val config: HistoryOptions by additionalProps
}